import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MyLexicalAnalyzer implements LexicalAnalyzer {
	int CurrentPosition = 0;
	char thisChar;
	boolean stopper = false;
	String currentCharacter = "";
	ArrayList<String> list = new ArrayList<String>();
	char nextCharacter;
	int c;
	File file = new File("/Users/grksoccerpro/Desktop/test.txt");
	BufferedReader reader = null;

	public void getNextToken() throws IOException, CompilerException {
		getCharacter();
	}

	// Gets each character from the string
	public void getCharacter() throws IOException, CompilerException {
			while (CurrentPosition < Compiler.stringbuilder.length()) {
				thisChar = Compiler.stringbuilder.charAt(CurrentPosition);
				currentCharacter = Character.toString(thisChar);
				addCharacter();
				System.out.println(Compiler.token);
				CurrentPosition++;
			}
		}
	
	/* THIS IS WHERE I ENCOUNTER THE ERRORS!!!
	   After it checks the whole file it then goes to the syntax analyzer. I want it to go to the syntax analyzer 
	   after it finds an annotation, 
	   but it wasn't working. */
	public void addCharacter() throws IOException, CompilerException {
		if (currentCharacter.equals("#")) {
			while (CurrentPosition < Compiler.stringbuilder.length()) {
				thisChar = Compiler.stringbuilder.charAt(CurrentPosition);
				currentCharacter = Character.toString(thisChar);
				System.out.println(Compiler.token);
				CurrentPosition++;
				if (lookupToken() == true && !Compiler.token.isEmpty()) {
					System.out.println(lookupToken());
					if (Compiler.token.isEmpty()) {
						Compiler.token = "";
						stopper = true;
					} else {
						Compiler.token = "";
					}
				}
				if (lookupToken() == false && currentCharacter.equalsIgnoreCase("#")) {
					System.out.println(lookupToken());
					throw new IOException("NOT LEGAL");
				} else
					lookupToken();
					System.out.println(lookupToken());

				if (currentCharacter.equalsIgnoreCase("#")) {
					if (lookupToken() == false)
						Compiler.token = "";
					Compiler.token += currentCharacter;
				} else {
					Compiler.token += currentCharacter;
				}

			}
		}
	}

	public boolean isSpace(String c) {
		if (c.equals(" ") | c.equals("\t") | c.equals("\n")) {
			return true;
		}
		return false;
	}

	public boolean lookupToken() throws IOException {
		String startdoc = "#START DOCUMENT";
		String enddoc = "#END DOCUMENT";
		String starthead = "#START HEAD";
		String endhead = "#END HEAD";
		String starttitle = "#START TITLE";
		String endtitle = "#END TITLE";
		String startpara = "#START PARAGRAPH";
		String endpara = "#END PARAGRAPH";
		String startbold = "#START BOLD";
		String endbold = "#END BOLD";
		String startital = "#START ITALICS";
		String endital = "#END ITALICS";
		String startlist = "#START LIST";
		String endlist = "#END LIST";
		String startitem = "#START ITEM";
		String enditem = "#END ITEM";
		String newline = "#NEWLINE";
		String startaud = "#START AUDIO";
		String endaud = "#END AUDIO";
		String startvid = "#START VIDEO";
		String endvid = "#END VIDEO";
		String startdef = "#START DEFINE";
		String enddef = "#END DEFINE";
		String name = "#NAME";
		String value = "#VALUE";
		String startuse = "#START USE";
		String enduse = "#END USE";

		list.add(startdoc);
		list.add(enddoc);
		list.add(starthead);
		list.add(endhead);
		list.add(starttitle);
		list.add(endtitle);
		list.add(startpara);
		list.add(endpara);
		list.add(startbold);
		list.add(endbold);
		list.add(startital);
		list.add(endital);
		list.add(startlist);
		list.add(endlist);
		list.add(startitem);
		list.add(enditem);
		list.add(newline);
		list.add(startaud);
		list.add(endaud);
		list.add(startvid);
		list.add(endvid);
		list.add(startdef);
		list.add(enddef);
		list.add(name);
		list.add(value);
		list.add(startuse);
		list.add(enduse);

		if (list.contains(Compiler.token))
			return true;
		if (CurrentPosition == 0 || CurrentPosition == 1) {
			return true;
		}
		if (Compiler.token.equals(" ") | Compiler.token.equals("\t")
				| Compiler.token.equals("\n")) {
			return true;
		}
		if (!Compiler.token.startsWith("#")) {
			return true;
		}
		else
			return false;
	}

}
